import * as THREE from 'three'

export function Group() {


  const geometry = new THREE.BoxGeometry(20, 20, 20)
  const material = new THREE.MeshLambertMaterial({ color: 0x00ffff });

  const mash1 = new THREE.Mesh(geometry, material);
  const mash2 = new THREE.Mesh(geometry, material);
  mash2.translateX(50)
  // const group = new THREE.Group()
  // group.add(mash1, mash2);
  const group = new THREE.Object3D()
  group.add(mash1, mash2);
  return group;
}


export function GroupTraverse() {
  const group1 = new THREE.Group();
  group1.name = '高层'
  for (let index = 0; index < 5; index++) {
    const geometry = new THREE.BoxGeometry(20, 60, 10);
    const material = new THREE.MeshLambertMaterial({
      color: 0x00ffff
    })
    const mesh = new THREE.Mesh(geometry, material);
    mesh.position.x = index * 30;
    mesh.name = `${index + 1}号楼`;
    group1.add(mesh);
  }
  group1.position.y = 30;


  const group2 = new THREE.Group();
  group2.name = '洋房'
  for (let index = 0; index < 5; index++) {
    const geometry = new THREE.BoxGeometry(20, 30, 10);
    const material = new THREE.MeshLambertMaterial();
    const mesh = new THREE.Mesh(geometry, material);
    mesh.position.x = index * 30;
    mesh.name = `${index + 1}号楼`;
    group2.add(mesh);
  }
  group2.position.z = 50;
  group2.position.y = 15;


  const model = new THREE.Group();
  model.name = '小区房子'
  model.add(group1, group2);
  return model;
}


export function coordinate() {
  const geometry = new THREE.BoxGeometry(20, 20, 20);
  const material = new THREE.MeshLambertMaterial({ color: 0x00ffff })
  const mesh = new THREE.Mesh(geometry, material);
  mesh.position.set(50, 0, 0);


  const meshAxesHelper = new THREE.AxesHelper(50);
  mesh.add(meshAxesHelper);


  const group = new THREE.Group();
  group.add(mesh);
  group.position.set(50, 0, 0);

  const worldPosition = new THREE.Vector3();
  mesh.getWorldPosition(worldPosition);
  console.log('世界坐标', worldPosition);
  console.log('本地坐标', mesh.position);

  return group;
}


export function texture() {
 const geometry = new THREE.PlaneGeometry(200, 100)
  // const geometry = new THREE.BoxGeometry(20, 10, 20)
 // const geometry = new THREE.SphereGeometry(600, 100, 100); //球体
  // const geometry = new THREE.CircleGeometry(60, 100);
  const textureLoader = new THREE.TextureLoader();
  const texture = textureLoader.load('./Earth.png')
  texture.wrapS = THREE.RepeatWrapping;
  // const texture = textureLoader.load('./images.jpeg')
  // texture.wrapS = THREE.RepeatWrapping;
  // texture.wrapT = THREE.RepeatWrapping;
  // // uv两个方向纹理重复数量
  // texture.repeat.set(20, 10);//注意选择合适的阵列数量


  // const uvs = new Float32Array([
  //  0,0.5,
  //  0.5,0.5,
  //  0,0,
  //  0.5,0,
  // ])
  // const uvs = new Float32Array([
  //   0,1,
  //   1,1,
  //   0,0,
  //   1,0,
  //  ])
  // geometry.attributes.uv = new THREE.BufferAttribute(uvs, 2)
  
  const material = new THREE.MeshLambertMaterial({
    // color: 0x00ffff,
    map: texture,
    side: THREE.DoubleSide,
    // transparent: true,
    // opacity: 0.5
  })

  const mesh = new THREE.Mesh(geometry, material)

// console.log('uv', geometry.attributes.uv, mesh, mesh.material.map )

  return mesh;
}
